﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace Roovent.HttpOperator
{
	static public class HttpTransfer
	{
		static public string SubmitForm(string uri, string parameters)
		{
			byte[] data = Encoding.ASCII.GetBytes(parameters);

			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
			request.Method = "POST";
			request.ContentType = "application/x-www-form-urlencoded";
			request.ContentLength = data.Length;

			try
			{
				Stream netStream = request.GetRequestStream();

				netStream.Write(data, 0, data.Length);
				netStream.Close();
			}
			catch (WebException)
			{
				throw;
			}

			HttpWebResponse response = (HttpWebResponse)request.GetResponse();

			StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
			string content = reader.ReadToEnd();

			reader.Close();
			response.Close();

			return content;
		}

		static public string SubmitForm(HtmlForm formLabel)
		{
			return SubmitForm(formLabel.Action, formLabel.Parameters);
		}
	}
}
